package com.yun.baymax.activity.account;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;

import com.yun.baymax.R;
import com.yun.baymax.activity.BaseActivity;
import com.yun.baymax.bean.User;
import com.yun.baymax.cache.UserCache;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by xupanpan on 1/6/17.
 */
public class ChangePasswordActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.change_password_old)
    TextInputLayout changePasswordOld;
    @BindView(R.id.change_password_new)
    TextInputLayout changePasswordNew;
    @BindView(R.id.change_password)
    Button changePassword;
    boolean oldInput;
    boolean newInput;
    private String newPwd;

    @Override
    protected int setLayoutResourceId() {
        return R.layout.activity_change_pwd;
    }

    @Override
    protected void initVariables() {
        ButterKnife.bind(this);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        changePasswordOld.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    oldInput = true;
                    if (newInput) {
                        changePassword.setSelected(true);
                        changePassword.setClickable(true);
                    }
                } else {
                    oldInput = false;
                    changePassword.setSelected(false);
                    changePassword.setClickable(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        changePasswordNew.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    newInput = true;
                    if (oldInput) {
                        changePassword.setSelected(true);
                        changePassword.setClickable(true);
                    }
                } else {
                    newInput = false;
                    changePassword.setSelected(false);
                    changePassword.setClickable(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void loadData() {

    }

    @OnClick(R.id.change_password)
    public void onClick() {

        String oldPwd = changePasswordOld.getEditText().getText().toString().trim();

        if (TextUtils.isEmpty(oldPwd)) {
            changePasswordOld.setError(getString(R.string.activity_change_old_password));
            changePasswordOld.setErrorEnabled(true);
            return;
        }
        changePasswordOld.setErrorEnabled(false);
        newPwd = changePasswordNew.getEditText().getText().toString().trim();

        if (TextUtils.isEmpty(newPwd)) {
            changePasswordNew.setError(getString(R.string.activity_change_new_password));
            changePasswordNew.setErrorEnabled(true);
            return;
        }

        changePasswordNew.setErrorEnabled(false);
        View view = getCurrentFocus();
        ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).
                hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

        BmobUser.updateCurrentUserPassword(oldPwd, newPwd, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    showToast("修改成功");
                    User currentUser = UserCache.getInstance().getCurrentUser();
                    currentUser.setPsw(newPwd);
                    UserCache.getInstance().updateUser(currentUser);
                    finish();
                } else {
                    showToast(e.getMessage());
                }
            }
        });
    }
}
